﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Yipusoft.SecurityEvaluate;
using System.Data;

public partial class Police_SecurityEvaluate_ParkEntirety : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        BindDate();
        BindData();
    }

    void BindDate()
    {
        for (int i = 2009; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));

        }
        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        BindWeek();
        BindWeekView();
    }

    void BindWeek()
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        for (int i = 1; i < GetWeekAmount(year); i++)
        {
            ddlWeek.Items.Add(new ListItem("第" + i + "周", i.ToString()));
        }
        ddlWeek.SelectedValue = (GetWeekOfYear(DateTime.Now) - 1).ToString();
    }

    /// <summary>
    /// 当前为该年中的第几周
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    int GetWeekOfYear(DateTime dt)
    {
        GregorianCalendar gc = new GregorianCalendar();
        return gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
    }
    /// <summary>
    /// 获取某年中有多少周
    /// </summary>
    /// <param name="year">年</param>
    /// <returns></returns>
    int GetWeekAmount(int year)
    {
        DateTime end = new DateTime(year, 12, 31);
        System.Globalization.GregorianCalendar gc = new GregorianCalendar();
        return gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindWeek();
        BindWeekView();
        //梆定数据
        BindData();
    }

    string GetWeekOfStartAndEnd(int year, int week)
    {

        DateTime YearOfOne = new DateTime(year, 1, 1);
        int dayofWeek = Convert.ToInt32(YearOfOne.DayOfWeek);
        if (dayofWeek == 0)
        {
            dayofWeek = 7;
        }
        DateTime StartDate = YearOfOne.AddDays((week - 1) * 7).AddDays(-dayofWeek + 1);
        DateTime EndDate = StartDate.AddDays(6);
        return StartDate.ToString("MM月dd日") + "--" + EndDate.ToString("MM月dd日");
    }
    protected void ddlWeek_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindWeekView();

        BindData();

    }

    void BindWeekView()
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int week = Convert.ToInt32(ddlWeek.SelectedValue);
        Caption.Text = "(" + GetWeekOfStartAndEnd(year, week) + "）车场日登记整体情况查看";
    }
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        ddlWeek.SelectedValue = Convert.ToString(GetWeekOfYear(DateTime.Now) - 1);
        BindWeekView();
        BindData();
    }

    void BindData()
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int week = Convert.ToInt32(ddlWeek.SelectedValue);
        DataTable dt = Park.GetParkEntirety(year, week);
        DataView dv = dt.DefaultView;
        UserChart.Series["车辆损坏个数"].Points.DataBindXY(dv, "ShortName", dv, "count");
        for (int i = 0; i < dv.Table.Rows.Count; i++)
        {
            UserChart.Series["车辆损坏个数"].Points[i].Url = "ParkCaption.aspx?DepartmentID=" + dv.Table.Rows[i]["DepartmentId"] + "&Week=" + ddlWeek.SelectedValue + "&Year=" + ddlYear.SelectedValue;
        }
    }
}
